﻿<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="CustomEmailLink"
                Location="CommandUI.Ribbon"
                Title="EmailLink">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.Documents.Share.Controls._children">
          <Button
              Id="Ribbon.Documents.Share.EmailItemLink"
              Sequence="10"
              Command="EmailLink2013"
              Image16by16="/_layouts/15/$Resources:core,Language;/images/formatmap16x16.png?rev=23" Image16by16Top="-115" Image16by16Left="-19"
              Image32by32="/_layouts/15/$Resources:core,Language;/images/formatmap32x32.png?rev=23" Image32by32Top="-137" Image32by32Left="-511"
              LabelText="$Resources:core,cui_ButEmailLink;"
              ToolTipTitle="$Resources:core,cui_ButEmailLink;"
              ToolTipDescription="$Resources:core,cui_STT_ButEmailLinkDocument;"
              TemplateAlias="o1"
                />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="EmailLink2013"
                          CommandAction="javascript:
                              var file;
                              function mailItemUrl() 
                              { 
                                  var mailcontext = SP.ClientContext.get_current();
                                  var web = mailcontext.get_web();
                                  var list = web.get_lists().getById(SP.ListOperation.Selection.getSelectedList())
                                  
                                  var items = SP.ListOperation.Selection.getSelectedItems();
                                  var item = list.getItemById(items[0].id);
                                  file = item.get_file();
                                  
                                  mailcontext.load(file);
                                  mailcontext.executeQueryAsync(onQuerySucceed, onQueryFailed);                                
                              }
                              
                              function onQuerySucceed(sender, args)
                              {
                                  var url = window.location.hostname + file.get_serverRelativeUrl();
                                  var formattedUrl1 = '';
                                  var parts = url.split('/');
                                  parts.forEach(function(y) {
                                     formattedUrl1 += encodeURIComponent('/') + escape(encodeURIComponent(y));
                                  });
                                  
                                  formattedUrl1 = encodeURIComponent('http://') + formattedUrl1;
                                
                                  
                                  window.location.href = 'mailto:?body=' + formattedUrl1;
                              }

                              function onQueryFailed(sender, args)
                              {
                                  alert('Request failed. ' + args.get_message() +
                                  '\n' + args.get_stackTrace());
                              }

                              mailItemUrl();"
                          EnabledScript="javascript:
                              function enableEmailLinkCommand()
                              {
                                  var items = SP.ListOperation.Selection.getSelectedItems();
                                  return (items.length == 1);                                  
                              }
                              enableEmailLinkCommand();
                          " />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
</Elements>